package com.tavenli.repository;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class RedisDaoTemplate<T> {
	private static final Class T = null;
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	// 注入模板为ListOperations.
	@Resource(name = "redisTemplate")
	private ListOperations<String, String> listOps;

	public void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	public T getValue(String keyStr) {
		return (T) redisTemplate.opsForValue().get(keyStr);
	}

	public void setValue(String t, String keyStr) {
		redisTemplate.opsForValue().set(keyStr, t);
	}
	public boolean hasKey(String key) {
		return redisTemplate.hasKey(key);
	}
	
	public void hashPut(String ObjectKey, String keyStr, Object object) {
		redisTemplate.opsForHash().put(ObjectKey, keyStr, object);
	}

	public void hashDelete(T key, String ObjectKey, String keyStr) {
		redisTemplate.opsForHash().delete(ObjectKey, keyStr);
	}

	public Object hashGet(String ObjectKey, String keyStr) {
		if (redisTemplate.hasKey(ObjectKey))
			return redisTemplate.opsForHash().get(ObjectKey, keyStr);
		else {
			return null;
		}
	}

	public Map<Object, Object> hashGetAll(String ObjectKey) {
		if (redisTemplate.hasKey(ObjectKey)) {
			return redisTemplate.opsForHash().entries(ObjectKey);
		} else
			return null;
	}

	public Long listLeftPush(String key, String value) {
		return redisTemplate.boundListOps(key).leftPush(value);
		//return listOps.leftPush(key, value);
	}
	public String  listRightPop(String key) {
		return redisTemplate.boundListOps(key).rightPop();
	}
	
	
	public List<String>  listTop1(String key) {
		return redisTemplate.boundListOps(key).range(0,1);
	}
	
	
	
	
	public long  listSize(String key) {
		return redisTemplate.boundListOps(key).size();
	}
	
	
	public Set<String>  getKeysPattern(String key) {
		return redisTemplate.keys(key+"*");
	}
	
	
	
	
}
